package com.example.waternetwork.api.system.controller;

import com.example.waternetwork.api.service.IResDispatch;
import com.example.waternetwork.api.system.entity.LoginUser;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import com.example.waternetwork.api.system.service.IUserService;
import com.example.waternetwork.api.model.R;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author auto gen
 * @since 2025-06-16
 */
@RestController
@RequestMapping("/system/user")
@Tag(name = "用户管理", description = "提供用户列表查询、分页获取用户信息等功能")
public class UserController {

    @Autowired
    private IUserService userService;
    /**
     * 获取用户列表
     * 
     * @return 用户列表
     */
    @GetMapping("/list")
    @Operation(summary = "获取用户列表", description = "返回所有用户的详细信息", responses = {
        @ApiResponse(responseCode = "200", description = "成功获取用户列表", content = @Content(schema = @Schema(implementation = R.class)))
    })
    public R list() {
        return R.ok(userService.list());
    }


    @GetMapping("/page")
    @Operation(summary = "分页获取用户信息", description = "根据当前页码和每页数量获取用户数据", responses = {
        @ApiResponse(responseCode = "200", description = "成功获取分页用户信息", content = @Content(schema = @Schema(implementation = R.class)))
    })
    public R page(@RequestParam("current")int current, @RequestParam("size") int size) {
        return R.ok(userService.getUserPage(current,size));
    }

    @GetMapping("/getUserInfo")
    @Operation(summary = "获取当前登录用户信息", description = "返回当前认证用户的详细信息", responses = {
        @ApiResponse(responseCode = "200", description = "成功获取当前用户信息", content = @Content(schema = @Schema(implementation = R.class)))
    })
    public R getUserInfo() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) auth.getPrincipal();
        return R.ok(loginUser);
    }

}